/*
 * @Author: xiaosihan
 * @Date: 2023-12-11 16:49:36
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-30 23:03:41
 */

import doubtfulSituationStore from "@views/DoubtfulSituation/doubtfulSituationStore";
import { Table } from "antd";
import { autorun, toJS } from "mobx";
import { ReactNode, useEffect, useRef, useState } from "react";
import siChuanMap3DStore from "../chengduMap3DStore";
import styles from "./hospitalInfo.module.less";
import medica_linstitution_portraitPNG from "./medica_linstitution_portrait.png";
import largeScreenApi from "@api/largeScreenApi";

interface Iprops {
    children?: ReactNode;
    active?: Number;
}

// 风险总览
export default function HospitalInfo(props: Iprops) {
    const { active = 0 } = props;
    let key = "";
    switch (active) {
        case 0:
            key = "医疗机构分析情况";
            break;
        case 1:
            key = "药店分析情况";
            break;
        case 2:
            key = "护理机构分析情况";
            break;
        case 3:
            key = "辅助器具机构分析情况";
            break;
    }
    const dom = useRef<HTMLDivElement | null>(null);
    const [data, setData] = useState(toJS(doubtfulSituationStore.activeMapData));
    const [now, setNow] = useState<any>({});

    const [areaCode] = useState(siChuanMap3DStore.areaCode);
    useEffect(
        () =>
            autorun(async () => {
                const { areaCode } = siChuanMap3DStore;
                const { mapActive, Xdata } = doubtfulSituationStore;
                let key = "";
                switch (mapActive) {
                    case 0:
                        key = "医疗机构分析情况";
                        break;
                    case 1:
                        key = "药店分析情况";
                        break;
                    case 2:
                        key = "护理机构分析情况";
                        break;
                    case 3:
                        key = "辅助器具机构分析情况";
                        break;
                }

                const res = (await largeScreenApi.risk_overview(key, areaCode === 510100 ? undefined : areaCode)) as any;
                const findXdata = res.list.find((x: { fixmedins_type: string; med_admdvs: number; }) => x.fixmedins_type === key && x.med_admdvs == areaCode) || {};
                setNow(findXdata);
                let allData = toJS(doubtfulSituationStore.activeMapData);
                let filterData: any = [];
                if (mapActive === 4) {
                    filterData = [{
                        "fixmedins_type": "护理机构分析情况",
                        "med_admdvs": "510117",
                        "med_admdvs_name": "郫都区",
                        "fixmedins_name": "成都郫都颐家护理站有限公司",
                        "rsk_cnt": "2"
                    }]
                } else if (areaCode == 510100) {
                    const res2 = await largeScreenApi.risk_organization_list(key, areaCode === 510100 ? undefined : areaCode) as any;
                    filterData = res2.list;
                    // allData.filter(
                    //     (o: { fixmedins_type: string }) => o.fixmedins_type == key
                    // );
                } else {
                    filterData = allData.filter(
                        (o: { fixmedins_type: string, med_admdvs: number }) => o.fixmedins_type == key && o.med_admdvs == areaCode
                    );
                }
                setData(filterData);
            }),
        []
    );

    return (
        <div className={styles.hospitalInfo}>
            <div className={styles.title}>风险总览</div>
            <div className={styles.subTitle}>
                风险机构总数 {now ? now.fixmedins_cnt : 0}家
            </div>
            <div className={styles.subTitle}>风险人数 {now ? now.psn_cnt : 0}人</div>
            <div className={styles.subTitle}>
                涉及风险线索记录 {now ? now.rsk_cnt : 0}条
            </div>

            {/* 在院人数列表 */}
            <div className={styles.in_hospital_list}>
                {
                    // (data.in_hospital_list.slice(0, 9) || []).map((l: any, i: number) => {
                    [].map((l: any, i: number) => {
                        return (
                            <div key={i} className={styles.listItem}>
                                <span className={styles.certno}>{l.certno}</span>
                                <span className={styles.psn_name}>{l.psn_name}</span>
                                <span className={styles.dept_name}>{l.dept_name}</span>
                            </div>
                        );
                    })
                }
                <Table
                    scroll={{ y: 510 }}
                    size="small"
                    className={styles.table}
                    columns={[
                        // {
                        //     title: "机构编号",
                        //     dataIndex: "institution_code",
                        //     key: "institution_code",
                        //     width: 140
                        // },
                        {
                            title: "机构名称",
                            dataIndex: "fixmedins_name",
                            key: "fixmedins_name",
                        },
                        {
                            title: "医保区划",
                            dataIndex: "med_admdvs_name",
                            key: "med_admdvs_name",
                            width: 100,
                        },
                        {
                            title: "触发模型数量",
                            dataIndex: "rsk_cnt",
                            key: "rsk_cnt",
                            align: "center",
                            width: 80,
                        },
                    ]}
                    dataSource={data.map((d: any, i: number) => ({ key: i, ...d }))}
                    pagination={false}
                />
                <img
                    className={styles.img}
                    src={medica_linstitution_portraitPNG}
                    alt=""
                />
            </div>
        </div>
    );
}
